﻿using IntelligentStorWpfApp.Helper;
using IntelligentStorWpfApp.Model;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using IntelligentStorWpfApp.Controller;
using System.Linq;
using log4net;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// inputProduct.xaml 的交互逻辑
    /// </summary>
    public partial class InportProduct : Window
    {
        public InportProduct()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
        private ProductController productControl;
        private List<ProductInfo> listPrds;
        private static readonly ILog log = LogManager.GetLogger(typeof(InportProduct));

        private void InportProduct_Loaded(object sender, RoutedEventArgs e)
        {
            productControl = new ProductController();
            listPrds = new List<ProductInfo>();
        }


        private void inportMenuItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var fd = new OpenFileDialog();
                fd.Filter = "Microsoft97-2003 Files(*.xls)|*.*";
                fd.FilterIndex = 2;
                fd.RestoreDirectory = true;
                fd.ShowDialog();


                if (!string.IsNullOrEmpty(fd.FileName))
                {
                    DataTable dt = ExcelHelper.getExcelData(fd.FileName);
                    listPrds = new List<ProductInfo>();
                    ProductInfo productInfo;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        productInfo = new ProductInfo();
                        productInfo.Owner = dt.Rows[i]["UserName"].ToString();
                        productInfo.ProductId = dt.Rows[i]["ProductId"].ToString();
                        productInfo.ProductNum = Convert.ToInt32(dt.Rows[i]["ProductNum"]);
                        productInfo.Status = dt.Rows[i]["Status"].ToString();
                        productInfo.Description = dt.Rows[i]["Description"].ToString();
                        listPrds.Add(productInfo);
                    }
                    listPrds.ForEach(p =>
                    {
                        p.Owner = App.user.UserName;
                    });
                    dgProducts.ItemsSource = listPrds.Distinct().ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                MessageBox.Show(ex.ToString());
            }
        }

        private void dgProducts_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            //judge = 1;
            //dgProducts.CanUserAddRows = true;
            //addListProd = new List<ProductInfo>();
            AddProduct addproWin = new AddProduct();
            addproWin.ShowDialog();
            if (!(addproWin.product.ProductId == null))
            {
                listPrds.Add(addproWin.product);
                dgProducts.ItemsSource = null;
                dgProducts.ItemsSource = listPrds.Distinct().ToList();
            }
            
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            List<ProductInfo> errList = new List<ProductInfo>();
            if(listPrds.Count>0)
            {
                MessageBoxResult result=MessageBox.Show("是否确认录入" + listPrds.Count.ToString() + "条数据？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if(result==MessageBoxResult.Yes)
                {
                    for (int i = 0; i < listPrds.Count; i++)
                    {
                        bool addRes = productControl.addProduct(listPrds[i]);
                        if(!addRes)
                        {
                            errList.Add(listPrds[i]);
                        }
                    }
                    if(errList.Count>0)
                    {
                        MessageBox.Show("有" + errList.Count.ToString() + "条数据，导入时发生异常！请检查","Error",MessageBoxButton.OK,MessageBoxImage.Error);
                        dgProducts.ItemsSource = null;
                        dgProducts.ItemsSource = errList;
                        return;
                    }
                    else
                    {
                        MessageBox.Show("数据导入成功!", "提示", MessageBoxButton.OK,MessageBoxImage.Information);
                        dgProducts.ItemsSource = null;
                    }

                }
            }
            else
            {
                MessageBox.Show("当前没有要导入的数据!","提示",MessageBoxButton.OK,MessageBoxImage.Warning);
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            listPrds = new List<ProductInfo>();
            dgProducts.ItemsSource = listPrds;
        }
    }
}
